﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BALayer;
using System.Data.SqlClient;

namespace QuanLyDoan
{
    public partial class Form1 : Form
    {
        DBDoanVien dbDangNhap = null;

        DataTable dtDangNhap;

        public Form1()
        {
            InitializeComponent();
            dbDangNhap = new DBDoanVien();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            string tenDangNhap = txtUser.Text.Trim();
            string matKhau = txtPass.Text.Trim();
            string mk = "";
            try
            {
                dtDangNhap = new DataTable();
                dtDangNhap.Clear();
                dtDangNhap = dbDangNhap.LayMatKhau(tenDangNhap).Tables[0];

                if (dtDangNhap.Rows.Count == 0)
                {
                    MessageBox.Show("Sai tên đăng nhập!!", "Thông báo");
                    txtUser.ResetText();
                    txtPass.ResetText();
                    txtUser.Focus();
                }
                else
                {
                    mk = dtDangNhap.Rows[0][0].ToString().Trim();
                    if (mk.Equals(matKhau))
                    {
                        Form fr = new FrmMain(tenDangNhap);
                        fr.ShowDialog();
                        this.Close();
                    }

                    else
                    {
                        MessageBox.Show("Sai mật khẩu!!", "Thông báo");
                        txtPass.ResetText();
                        txtPass.Focus();
                    }
                }
            }
            catch (SqlException)
            {

            }
        }

        private void txtUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) { txtPass.Focus(); }
        }

        private void txtPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) { this.btnDangNhap_Click(sender, e); }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult trove;
            trove = MessageBox.Show("Bạn có chắc chắn thoát khỏi chương trình không ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (trove == DialogResult.OK)
                Application.Exit();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtPass.ResetText();
            txtUser.ResetText();
            txtUser.Focus();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
